#include <core/core.h>



namespace yam3d
{
namespace core
{

Exception::Exception(const char* const message, const char* file, int line, const yam3d::core::string& details)
: m_details( makeErrorString(details,file,line,details) )
{
}

Exception::Exception(const yam3d::core::string& message, const char* file, int line, const yam3d::core::string& details )
: m_details( makeErrorString(details,file,line,details) )
{

}

Exception::Exception(const Exception& o )
    : m_details( new char[strlen(o.m_details)+1] )
{
    strcpy( m_details, o.m_details );
}

Exception& Exception::operator=(const Exception& o)
{
    if( this != &o )
    {
        delete [] m_details;
        m_details = new char[strlen(o.m_details)+1];
        strcpy(m_details, o.m_details);
    }
    return *this;
}

Exception::~Exception() throw()
{
    delete [] m_details;
}

const char* Exception::what() const throw()
{
        return m_details;
}

char* Exception::makeErrorString(const yam3d::core::string& message, yam3d::core::string file, int line, const yam3d::core::string& details )
{
    file = file.toStdString().substr( file.toStdString().find_last_of( "\\/" ) + 1 ).c_str();
    yam3d::core::string fullMessage = message;
    fullMessage += " <";
    fullMessage += file;
    fullMessage += "@";
    fullMessage += core::string::number(line);
    fullMessage += "> ";
    fullMessage += details;
    LOG(fullMessage.toStdString().c_str());

    char* res = new char[fullMessage.length()+1];
    strcpy( res, fullMessage.toStdString().c_str() );
    return res;
}


}
}

